package com.alibaba.base.user.controller;


import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.alibaba.base.user.model.User;
import com.alibaba.base.user.servier.UserService;
import com.alibaba.base.util.JsonUtil;

@RestController
@RequestMapping("/user/")
public class UserController {
	
	@Autowired
	private UserService userService;
	
	@RequestMapping("addUser")
	public String addUser(User user)  {
		/*request.setCharacterEncoding("UTF-8");
		response.setCharacterEncoding("UTF-8");
		//告诉浏览器响应的是json，通过utf-8编码
		response.setContentType("application/json;charset=utf-8");*/
		Long i = userService.addUser(user);
		if(i!=0) {
			return JsonUtil.success();
		}
		return JsonUtil.error("添加失败");
	}
	
	@RequestMapping("updateUser")
	public String updateUser(User user) {
		Long i = userService.updateUser(user);
		if(i!=0) {
			return JsonUtil.success();
		}
		return JsonUtil.error("修改失败");
	}
	
	@RequestMapping("getUser")
	public String gettUser() {
		return JsonUtil.success(userService.gettUser());
	}
	
	@RequestMapping("getUserById")
	public String getUserById(Long id) {
		
		return JsonUtil.success(userService.getUserById(id));
	}
	
	@RequestMapping("deleteUser")
	public String deleteUser(Long id) {
		Long i = userService.deleteUser(id);
		
		if(i!=0) {
			return JsonUtil.success();
		}
		return JsonUtil.error("删除失败");
	}
	
	

}
